import time
import hashlib
from server.func.database import get_user_password

tmp_key = {}


def md5(s: str):
    m = hashlib.md5()
    m.update(s.encode("utf8"))
    return m.hexdigest()


def verify_user(uid, password):
    if get_user_password(uid) == password:
        return True
    else:
        return False


def create_temp_key(uid):
    global tmp_key
    tmp_key[uid] = md5(str(time.time())+uid)
    return tmp_key[uid]


def verify_key(uid, key):
    global tmp_key
    if uid in tmp_key.keys() and tmp_key[uid] == key:
        return True
    else:
        return False

